<template>
    <!--左侧房源筛选-->
    <div class="lefMain">
        <!--左侧1-->
        <span class="leftTop">
            <span style="font-size: 18px;white-space: nowrap;color:#7a8df1;font-weight: 600;">{{ $t('text.Contract.l79')
            }}</span>
            <span class="df">
            </span>
        </span>
        <div class="leftTopInput">
            <img class="img1" src="../../assets/images/search2.png" />
            <input :placeholder="$t('text.Contract.l74')" class="leftTop2Input" v-model="nameOrNo">
        </div>
        <!--左侧3-->
        <div>
            <div class="leftTop2">

                <div class="df">
                    <img class="img2" src="../../assets/images/icon.png" />
                    <div class="time">
                        {{ $t('text.Contract.l80') }}
                        <el-date-picker v-model="time" type="month" :placeholder="$t('text.Employee.l26')"
                            style="width: 100px;margin-left: 10px;height: 23px;font-size: 12px;" value-format="YYYY-MM" />
                    </div>
                </div>

                <div class="df">
                    <img class="img2" src="../../assets/images/icon.png" />
                    <div class="items">
                        <RadioSelect :leftText="$t('text.Contract.l67')" :selectList="orgTreeList" searchKey="orgIds"
                            isShowTitle @returnData="setData" @clearData="clearData" isRange2 :isReset="isReset">
                        </RadioSelect>
                    </div>
                </div>

            </div>
        </div>
        <div class="resetDiv" @click="reset">{{ $t('text.Contract.l32') }}</div>

    </div>

    <!--右侧房源列表-->
    <div class="rightMain">
        <!--右侧顶部导航-->
        <div class="rightTop">
            <div class="out">
                <img class="outImg" src="../../assets/images/out.svg">
                <span>{{ $t('text.Contract.l90') }}</span>
            </div>
            <div></div>
            <div class="out" v-if="!review && resultList.length != 0">
                <div class="outAgain" @click="generateSalary">
                    <img src="../../assets/images/refresh.svg">
                    {{ $t('text.Contract.l91') }}
                </div>
                <div class="outComfirm" @click="examineSalary ">{{ $t('text.Contract.l92') }}</div>
            </div>
            <div class="out" v-if="review" style="color: #2196F3;">
                {{ $t('text.Contract.l352') }}
            </div>
        </div>
        <!--右侧房源数据列表-->
        <div class="generate" v-if="!review && resultList.length == 0">
            <div style="font-size: 20px;padding-bottom: 10px;">{{ $t('text.Contract.l353') }}</div>
            <div style="color: #2196F3;" @click="generateSalary">{{ $t('text.Contract.l354') }}</div>
        </div>
        <div class="rightContain" v-else>

            <table class="rightList" v-if="resultList.length > 0">

                <tr class="eDataList dataHead">
                    <td class="dataDom" v-for="(item, index) in dataTitleList" v-bind:key="item"
                        :style="index == 0 || index == 1 ? { flex: 1.5 } : {}">
                        <div class="dataTitle">{{ item }}</div>
                    </td>
                </tr>

                <tr class="eDataList" v-for="(item) in resultList" v-bind:key="item">
                    <td class="dataDom" style="margin-top: -5px;flex: 1.5;">
                        <div class="dataTitle">
                            <CommonImage :src="item.avatar" class="imgStyle" />
                            <CommonOmit :text="item.userName == null ? '-' : item.userName" :length="1"></CommonOmit>
                        </div>
                    </td>
                    <td class="dataDom" style="flex: 1.5;">
                        <div class="dataTitle" style="white-space: pre-wrap;">
                            <CommonOmit :text="item.orgName + '-' + item.jobName" :length="1"></CommonOmit>
                        </div>
                    </td>
                    <td class="dataDom">
                        <div class="dataTitle">{{ item.fixedPrice == null ? '-'
                            : String(item.fixedPrice).replace(/\B(?=(\d{3})+(?!\d))/g, ',') }}</div>
                    </td>
                    <td class="dataDom">
                        <div class="dataTitle">{{ item.meritPrice == null ? '-'
                            : String(item.meritPrice).replace(/\B(?=(\d{3})+(?!\d))/g, ',') }}</div>
                    </td>
                    <td class="dataDom">
                        <div class="dataTitle">{{ item.attendPrice == null ? '-'
                            : String(item.attendPrice).replace(/\B(?=(\d{3})+(?!\d))/g, ',') }}</div>
                    </td>
                    <td class="dataDom">
                        <div class="dataTitle">{{ item.rewardPrice == null ? '-'
                            : String(item.rewardPrice).replace(/\B(?=(\d{3})+(?!\d))/g, ',') }}</div>
                    </td>
                    <td class="dataDom">
                        <div class="dataTitle">{{ item.deductPrice == null ? '-'
                            : String(item.deductPrice).replace(/\B(?=(\d{3})+(?!\d))/g, ',') }}</div>
                    </td>
                    <td class="dataDom">
                        <div class="dataTitle">{{ getTotalPrice(item) }}</div>
                    </td>
                    <td class="dataDom">
                        <div class="dataTitle">{{ item.month == null ? '-': item.month }}</div>
                    </td>
                </tr>
            </table>
            <div class="rightList" v-else>
                <div class="nulData">
                    {{ $t('text.Common.l67') }}
                </div>
            </div>
            <!--底部分页-->
            <div class="setPage">
                <el-pagination v-model:current-page="searchData.pageNo" v-model:page-size="searchData.pageSize"
                    :page-sizes="[10, 15, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    @size-change="handleSizeChange" @current-change="handleCurrentChange" />
            </div>
        </div>
    </div>
</template>
    
<script>
import { ElMessage } from 'element-plus'
import CommonImage from "@/components/input/CommonImage.vue"
import RadioSelect from "../../components/select/RadioSelect.vue"
import { salaryManageFindPage, orgTreeNoUser, salaryManageGenerate ,salaryManageReview} from "../../http/api"
import CommonOmit from "@/components/input/CommonOmit.vue"

export default {
    name: "PayrollManage",
    components: {
        RadioSelect,
        CommonImage,
        CommonOmit
    },
    data() {
        return {
            searchData: {
                pageNo: 1,
                pageSize: 15,
                //month:new Date().getFullYear()+"-"+new Date().getMonth()+1
            },
            dictionaryTree: {},
            isReset: false,
            dataTitleList: [
                this.$t("text.Contract.l81"), this.$t("text.Contract.l82"), this.$t("text.Contract.l83"),
                this.$t("text.Contract.l84"), this.$t("text.Contract.l85"), this.$t("text.Contract.l86"),
                this.$t("text.Contract.l87"), this.$t("text.Contract.l88"), this.$t("text.Contract.l89"),
            ],
            resultList: [],
            total: 0,
            orgTreeList: [],
            time: "",
            nameOrNo: "",
            review: false
        }
    },
    mounted() {
        orgTreeNoUser().then(res => {
            if (res !== undefined) {
                this.orgTreeList = res
            }
        }).catch()
        this.search()
    },
    methods: {

        //获取合计价值
        getTotalPrice(item) {
            return item.fixedPrice + item.meritPrice + item.attendPrice + item.rewardPrice + item.deductPrice
        },

        //生成薪资
        generateSalary() {
            let month = this.searchData.month
            if (month==undefined||month=="") {
                month = this.nowMonth
            }
            console.log(month)
            salaryManageGenerate({ month: month }).then(res => {
                if (res !== undefined) {
                    this.search()
                    ElMessage.success({
                        showClose: true,
                        message: this.$t('text.Contract.l355'),
                        type: 'success',
                    })
                }
            })
        },

        //审核薪资
        examineSalary() {
            let month = this.searchData.month
            if (month==undefined||month=="") {
                month = this.nowMonth
            }
            salaryManageReview({ month: month }).then(res => {
                if (res !== undefined) {
                    this.search()
                    ElMessage.success({
                        showClose: true,
                        message: this.$t('text.Contract.l356'),
                        type: 'success',
                    })
                }
            })
        },

        //搜索数据
        search() {
            salaryManageFindPage(this.searchData).then(res => {
                if (res !== undefined) {
                    this.resultList = res.dataList
                    this.total = res.total
                    this.review = res.review
                    console.log(res)
                }
            })
        },

        //拼接单个子组件返回来的条件值
        setData(obj) {
            this.searchData = { ...this.searchData, ...obj }
            console.log(JSON.stringify(this.searchData))
        },

        //子组件选择了不限，进行删除指定字段
        clearData(obj) {
            const objKeys = Object.keys(obj)
            this.searchData = Object.keys(this.searchData).reduce((acc, key) => {
                if (!objKeys.includes(key)) {
                    acc[key] = this.searchData[key]
                }
                return acc
            }, {})
        },

        //重置
        reset() {
            this.searchData = {
                pageNo: 1,
                pageSize: 10,
            }
            this.time = ''
            this.isReset = !this.isReset
        },
        handleSizeChange(val) {
            this.searchData.pageSize = val
            this.search()
        },
        handleCurrentChange(val) {
            this.searchData.pageNo = val
            this.search()
        },
    },
    watch: {
        searchData: {
            handler() {
                this.search()
            },
        },
        time() {
            if (this.time == null) {
                this.clearData({ month: "" })
            } else {
                let obj = { month: this.time }
                this.searchData = { ...this.searchData, ...obj }
            }
        },
        nameOrNo() {
            let obj = { nameOrNo: this.nameOrNo }
            this.setData(obj)
        },
    },
    computed: {
        //获取当前月份
        nowMonth() {
            var date = new Date();
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            m = m < 10 ? ('0' + m) : m;
            return y + '-' + m;
        }
    },
}
</script>
    
<style scoped>
.imgStyle {
    min-width: 35px;
    width: 35px;
    height: 35px;
    border-radius: 100%;
    margin-right: 15px;
}

.nulData {
    background-image: url('../../assets/images/nullData.svg');
    background-size: cover;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 25px;
    color: #979797;
}

.resetDiv {
    background: #6698ff;
    margin-top: 20px;
    padding: 5px 0;
    font-size: 13px;
    color: white;
    border-radius: 7px;
}

.df {
    display: flex;
    align-items: center;
    margin-top: 8px;
    margin-bottom: 2px;
}

.lefMain {
    min-width: 310px;
    padding: 15px;
    overflow: auto;
    background: white;
    margin-bottom: 30px;
    margin-right: 5px;
    box-shadow: 1px 10px 30px 4px #e9e9e9;
}

.rightContain {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    background: white;
    margin: 20px 30px 30px 25px;
    position: relative;
    box-shadow: 1px 5px 10px 4px #e9e9e9;
}

.leftTop {
    color: #6698ff;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.leftTop2 {
    border: 1px solid #cecece;
    border-radius: 5px;
    padding: 10px;
    margin-top: 10px;
    position: relative;
    padding-top: 0;
}

.img2 {
    width: 22px;
    height: 22px;
}

.items {
    margin-left: 15px;
    padding-top: 6px;
    padding-bottom: 6px;
    font-size: 12px;
    color: gray;
    border-bottom: 1px solid rgb(224, 224, 224);
    width: 100%;
}

.rightMain {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    background: #f8f8f8;
}

.rightList {
    height: 100%;
    display: flex;
    flex-direction: column;
    border-radius: 5px;
    overflow: auto;
    padding: 20px 0;
    padding-top: 0;
}

.rightTop {
    display: flex;
    padding-top: 15px;
    align-items: center;
    justify-content: space-between;
    padding-right: 32px;
    padding-left: 32px;
}


.setPage {
    display: flex;
    justify-content: flex-end;
    border-top: 1px solid #eee;
    padding: 10px;
}

.out {
    display: flex;
    align-items: center;
    font-size: 14px;
}

.outImg {
    width: 20px;
    height: 20px;
    padding-right: 5px;
}

.outAgain {
    display: flex;
    align-items: center;
    margin-right: 15px;
    font-size: 12px;
    color: #6698ff;
}

.outAgain>img {
    width: 17px;
    height: 17px;
    margin-right: 4px;
}

.outComfirm {
    color: white;
    border-radius: 50px;
    background: #6698ff;
    font-size: 12px;
    padding: 2px 10px;
}

.img2 {
    width: 22px;
    height: 22px;
}

.time {
    margin-left: 15px;
    padding-top: 6px;
    padding-bottom: 6px;
    font-size: 12px;
    color: gray;
    border-bottom: 1px solid rgb(224, 224, 224);
    width: 100%;
    display: flex;
    align-items: center;
    white-space: nowrap;
    justify-content: space-between;
}

.eDataList {
    display: flex;
    align-items: flex-start;
    padding: 5px 30px;
    margin: 3px 0px;
    position: relative;
}

.eDataList::after {
    position: absolute;
    content: '';
    width: calc(100% - 60px);
    height: 1px;
    bottom: 0;
    border-bottom: 1px solid #eee;
    padding: 1px;
    margin-right: 30px;
}

.eDataList:hover {
    background: #eee;
}

.dataTitle {
    font-size: 13px;
    color: #1f1f1f;
    display: flex;
    min-height: 22px;
    align-items: center;
}

.dataDom {
    flex: 1;
    text-align: left;
    padding: 10px 0;
    display: flex;
    justify-content: flex-start;
    font-size: 13px;
}

.leftTopInput {
    display: flex;
    width: 100%;
    position: relative;
    padding-top: 10px;
}

.dataHead {
    background: rgb(238, 238, 238);
    margin-top: 0px;
    position: sticky;
    top: 0;
    z-index: 1;
}

.img1 {
    position: absolute;
    left: 7px;
    width: 15px;
    height: 15px;
    top: 17px;
    z-index: 1;
}

.leftTop2Input {
    outline: none;
    width: inherit;
    height: 25px;
    border: 1px solid rgb(220, 223, 230);
    padding-left: 23px;
    padding-right: 20px;
    background-color: rgb(242, 242, 242);
    border-radius: 5px;
    font-size: 12px;
    position: relative;
}

.generate {
    height: 85%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}
</style>